import lombok.SneakyThrows;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DataConverterUill {


	//传入时间字符串，判断周目标月目标获取时间段
	@SneakyThrows
	public static List<String> getDateQuantum(String type) {

		ArrayList<String> times = new ArrayList<>();
		switch (type) {
			//今天
			case "0":
				times.add(getDateFormat(new Date()));
				times.add(getDateFormat(new Date()));
				break;
			//明天
			case "1":
				times.add(getDateFormat(gettomorrow()));
				times.add(getDateFormat(gettomorrow()));
				break;
			//本周
			case "2":
				times.add(getDateFormat(getTimesWeekmorning()));
				times.add(getDateFormat(getTimesWeeknight()));
				break;
			//本月
			case "3":
				times.add(getDateFormat(getTimesMonthmorning()));
				times.add(getDateFormat(getTimesMonthnight()));
				break;
			//季度
			case "4":
				times.add(getDateFormat(getCurrentQuarterStartTime()));
				times.add(getDateFormat(getCurrentQuarterEndTime()));
				break;
			//半年度
			case "5":
				times.add(getDateFormat(getHalfYearStartTime()));
				times.add(getDateFormat(getHalfYearEndTime()));
				break;
			//年度
			case "6":
				times.add(getDateFormat(getCurrentYearStartTime()));
				times.add(getDateFormat(getCurrentYearEndTime()));
				break;
			default:
				break;
		}
		return times;
	}


	//时间转换（date转LocalDateTime）
	public static String getDateFormat(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(date);
	}

	// 获得明天的时间
	public static Date gettomorrow() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_WEEK, 1);
		return cal.getTime();
	}

	// 获得指定周周一0点时间
	public static Date getTimesWeekmorning() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return cal.getTime();
	}

	// 获得指定周周日0点时间
	public static Date getTimesWeeknight() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getTimesWeekmorning());
		cal.add(Calendar.DAY_OF_WEEK, 6);
		return cal.getTime();
	}

	// 获得本月第一天0点时间
	public static Date getTimesMonthmorning() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
		return cal.getTime();
	}

	// 获得本月最后一天24点时间
	public static Date getTimesMonthnight() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 0);
		return cal.getTime();
	}


	//本季度开始点时间
	public static Date getCurrentQuarterStartTime() {
		Calendar c = Calendar.getInstance();
		//获取当前月
		int m = c.get(Calendar.MONTH);
		try {
			if (0<=m && m<3) {
				c.set(Calendar.MONTH, 0);
			} else if (3<=m && m<6) {
				c.set(Calendar.MONTH, 3);
			} else if (6<=m && m<9) {
				c.set(Calendar.MONTH, 6);
			} else if (9<=m && m<12) {
				c.set(Calendar.MONTH, 9);
			}
			c.set(Calendar.DATE, 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c.getTime();
	}

	/**
	 * 当前季度的结束时间，即2012-03-31 23:59:59
	 *
	 * @return
	 */
	public static Date getCurrentQuarterEndTime() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getCurrentQuarterStartTime());
		cal.add(Calendar.MONTH, 2);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 0);
		return cal.getTime();
	}

	/**
	 * 获取前/后半年的开始时间
	 * @return
	 */
	public static Date getHalfYearStartTime(){
		Calendar cal = Calendar.getInstance();
		int currentMonth = cal.get(Calendar.MONTH) + 1;
		try {
			if (currentMonth >= 1 && currentMonth <= 6){
				cal.set(Calendar.MONTH, 0);
			}else if (currentMonth >= 7 && currentMonth <= 12){
				cal.set(Calendar.MONTH, 6);
			}
			cal.set(Calendar.DATE, 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cal.getTime();

	}
	/**
	 * 获取前/后半年的结束时间
	 * @return
	 */
	public static Date getHalfYearEndTime(){
		Calendar cal = Calendar.getInstance();
		int currentMonth = cal.get(Calendar.MONTH) + 1;
		Date now = null;
		try {
			if (currentMonth >= 1 && currentMonth <= 6){
				cal.set(Calendar.MONTH, 5);
				cal.set(Calendar.DATE, 30);
			}else if (currentMonth >= 7 && currentMonth <= 12){
				cal.set(Calendar.MONTH, 11);
				cal.set(Calendar.DATE, 31);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cal.getTime();
	}

	//本年开始点时间
	public static Date getCurrentYearStartTime() {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), 0, cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.YEAR));
		return cal.getTime();
	}

	//本年结束点时间
	public static Date getCurrentYearEndTime() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getCurrentYearStartTime());
		cal.set(Calendar.MONTH, 11);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 0);
		return cal.getTime();
	}


	/**
	 * 获取同一年两个时间节点之间的月份列表
	 * @param minDate
	 * @param maxDate
	 * @param flag true 返回月份，
	 * @return
	 */
	public static List<String> getMonthBetween(String minDate, String maxDate,boolean flag){
		ArrayList<String> result = new ArrayList<String>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
			SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");//格式化为月
			Calendar min = Calendar.getInstance();
			Calendar max = Calendar.getInstance();
			min.setTime(sdf.parse(minDate));
			max.setTime(sdf.parse(maxDate));
			//判断年份是否相同
			boolean isSameYear = min.get(Calendar.YEAR) == max
					.get(Calendar.YEAR);
			if (!isSameYear) {
				return result;
			}
			min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
			max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
			while (min.before(max)) {
				String format = sdfMonth.format(min.getTime());
				String month = String.valueOf(Integer.valueOf(format));
				if (flag) {
					result.add(month);
				}else {
					result.add(sdf.format(min.getTime()));
				}
				min.add(Calendar.MONTH, 1);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static List<String> getMonthBetween(String minDate, String maxDate){
		ArrayList<String> result = new ArrayList<String>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

			Calendar min = Calendar.getInstance();
			Calendar max = Calendar.getInstance();

			min.setTime(sdf.parse(minDate));
			min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

			max.setTime(sdf.parse(maxDate));
			max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

			Calendar curr = min;
			while (curr.before(max)) {
				result.add(sdf.format(curr.getTime()));
				curr.add(Calendar.MONTH, 1);
			}
		}catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	//获取一个月天数
	public static int getDaysOfMonth(String time) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
		Date date = null;
		try {
			date = simpleDateFormat.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		assert date != null;
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}


}
